﻿package com.rand.beauty;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import com.example.android.bitmapfun.util.DiskLruCache;

@SuppressLint("DefaultLocale")
public class KeyFilterUtils {

	private static int mYear = 2014;
	private static int mMonth = 4;
	private static int mDate = 24;

	private static boolean mbShowAD = true;

	public static boolean isShowAD() {
		return mbShowAD;
	}

	static public boolean isKeyWordFilter(String word) {
		String[] containsValue = { "性感", "乳", "丰满", "诱惑", "少妇", "胸", "大波",
				"内衣", "宅男", "丝袜", "裸", "激情", "寂寞", "私房", "湿身", "浴缸", "流氓",
				"自慰", "衫原杏", "小仓优子", "浴室", "模特", "诱人", "车模", "西洋", "妹子", "全部",
				"写真", "日韩", "湿身", "推荐" };
		int len = containsValue.length;
		for (int i = 0; i < len; ++i) {
			if (word.indexOf(containsValue[i]) != -1) {
				return true;
			}
		}

		return false;
	}

	public static boolean afterTime() {
		Calendar ca = Calendar.getInstance();
		int year = ca.get(Calendar.YEAR);
		int month = ca.get(Calendar.MONTH);
		int day = ca.get(Calendar.DATE);

		// 2014-1-6
		int after = (year - mYear) * 365 + ((month + 1) - mMonth) * 30 + day
				- mDate;

		if (after > 0) {
			return true;
		}
		return false;
	}

	public static boolean isCanShow(String className) {
		if (!afterTime() && isKeyWordFilter(className)) {
			return false;
		}
		return true;
	}

	public static boolean isFileInFavList(Context context, String url) {
		final File cacheDir = DiskLruCache
				.getDiskCacheDir(context, "favourite");

		final DiskLruCache cache = DiskLruCache.openCache(context, cacheDir,
				50 * 1024 * 1024);
		String fileName = MD5(url) + ".jpg";
		return cache.containsKey(fileName);
	}

	public static String MD5(String string) {
		byte[] hash;
		try {
			hash = MessageDigest.getInstance("MD5").digest(
					string.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}

		StringBuilder hex = new StringBuilder(hash.length * 2);
		for (byte b : hash) {
			if ((b & 0xFF) < 0x10)
				hex.append("0");
			hex.append(Integer.toHexString(b & 0xFF));
		}
		return hex.toString();
	}

	public static void SaveImageInfo(String filename, int height, int width,
			Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				"favouriteslist", 0);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putInt(filename + "height", height);
		editor.putInt(filename + "width", width);
		editor.commit();
	}

	public static ImageSize GetImageInfo(String filename, Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				"favouriteslist", 0);
		ImageSize size = new ImageSize();
		size.mHeight = sharedPreferences.getInt(filename + "height", 1);
		size.mWidth = sharedPreferences.getInt(filename + "width", 1);
		return size;
	}

	public static void ClearImageInfo(String filename, Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				"favouriteslist", 0);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.remove(filename + "height");
		editor.remove(filename + "width");
		editor.commit();
	}

	public static int getCacheSize(Context context) {
		final File cacheClassDir = DiskLruCache.getDiskCacheDir(context,
				"classlist");

		final File cacheDetailDir = DiskLruCache.getDiskCacheDir(context,
				"detaillist");

		try {
			return (int) (getFileSize(cacheClassDir) + getFileSize(cacheDetailDir));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	public static long getFileSize(File f) throws Exception {
		long size = 0;
		if (f != null) {
			File flist[] = f.listFiles();
			for (int i = 0; flist != null && i < flist.length; i++) {
				if (flist[i].isDirectory()) {
					size = size + getFileSize(flist[i]);
				} else {
					size = size + flist[i].length();
				}
			}
		}

		return size;
	}

	@SuppressLint("DefaultLocale")
	public static String formatSize(int size) {
		long kb = 1024;
		long mb = (kb * 1024);
		long gb = (mb * 1024);
		if (size < kb) {
			return String.format("%dB", (int) size);
		} else if (size < mb) {
			return String.format("%.2fKB", (float) size / kb); // 保留两位小数
		} else if (size < gb) {
			return String.format("%.2fMB", (float) size / mb);
		} else {
			return String.format("%.2fGB", (float) size / gb);
		}
	}

	public static String getCacheSizeInString(Context context) {
		return formatSize(getCacheSize(context));
	}
}
